<?php

namespace app\admin\controller\formdesign;

use addons\formdesign\library\FormDesignApi;
use addons\formdesign\library\Formsafe;
use app\common\controller\Backend;
use think\Db;
class Foreign extends Backend
{

    /**
     * Foreign模型对象
     * @var \app\common\model\formdesign\foreign
     */
    protected $noNeedLogin = []; //无需登录的方法,同时也就不需要鉴权了
    protected $noNeedRight = []; //无需鉴权的方法,但需要登录

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('app\common\model\formdesign\Foreign');
        Formsafe::safe_gpc();
    }

    /**
     * 查看
     */
    public function index()
    {
        $form_id = $this->request->param('ids') ?: $form_id = $this->request->param('form_id');
        $FormDesign = new FormDesignApi($form_id);
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            return json($FormDesign->getList($where, $sort, $order, $offset, $limit));
        }
        $this->assign('form_id', $form_id);
        $this->assignconfig('columns', $FormDesign->getFields(true));
        return $this->view->fetch();
    }

    public function view()
    {
        $id = $this->request->param('ids');
        $FormDesign = new FormDesignApi(null, $id);
        $form = $FormDesign->view($id)->getForm();
        $this->assign('form', $form);
        $this->view->engine->layout('layout/default');
        return $this->view->fetch();
    }

    public function add()
    {
        $this->request->filter(['strip_tags']);
        $form_id = $this->request->param('form_id');
        $aid=input('get.aid/d');//aboutid,otherdataid，相关表id
        $t=input('get.t/s');//代表表单的类型，raw=clothmaking_raw
     
        $FormDesign = new FormDesignApi($form_id);
        if ($this->request->isAjax()) {
            $did=null;
           
               $formData=$this->model->where('form_id',$form_id)->where('name',$t)->find();
               if($formData!=null)
               {
                // $obj_model = db('formdesign_form_data_' . $form_id);
                //  $data= $obj_model->where('foreign_id',$formData['id'])->where('about_dataid',$aid)->find();               
                //  if($data!=null)
                //  {
                //      $did=$data["id"];                  
                //  }
                $did=$formData['id'];
                
               }
            
            $FormDesign->setData($this->request->post(),$did,$t,$aid);                  
         
            }
        $this->assign('form', $FormDesign->add()->getForm());
        $this->view->engine->layout('layout/default');
        return $this->view->fetch();
    }

    public function edit($ids = null)
    {
        $this->request->filter(['strip_tags']);
        $id = $this->request->param('ids');
        $FormDesign = new FormDesignApi(null, $id);
        if ($this->request->isAjax()) {
            $FormDesign->setData($this->request->post(), $id);
        }
        $this->assign('form', $FormDesign->edit($id)->getForm());
        $this->view->engine->layout('layout/default');
        return $this->view->fetch();
    }

    public function del($ids = "")
    {
        $tables = explode(',', $ids);
        $form_id = $this->request->param('form_id');
        foreach ($tables as $id) {
            db('formdesign_form_data_' . $form_id)->where('foreign_id', $id)->delete();
        }
        parent::del($ids);
    }

    public function query()
    {
        $this->request->filter(['strip_tags']);
        $form_id = $this->request->param('form_id');
        $FormDesign = new FormDesignApi($form_id);
        if ($this->request->isAjax()) {
            if ($this->request->request('keyField')) {
                $fields = $FormDesign->getFields();
                $box = [];
                foreach ($fields as $key => $value) {
                    array_push($box, ['id' => $key, 'name' => $value['title']]);
                }
                return $box;
            }
            $query = $this->request->post('query/a');
            $FormDesign->setQueryFields($query);
        }
        $select = $FormDesign->getQueryFields();
        $this->assign('form_id', $form_id);
        $this->assign('value', $select);
        $this->view->engine->layout('layout/default');
        return $this->view->fetch();
    }
}
